package com.snailvr.tv.ui.adapter;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.snail.readyvr.R;
import com.snail.readyvr.util.LogUtils;
import com.snail.readyvr.util.SPUtils;
import com.snailvr.tv.Definition;

import java.util.List;

public class ProjectionAdapter extends ArrayAdapter<ProjectionListItem>{
    private SparseArray<ImageView> mAllImgs = new SparseArray<>();
    private View.OnClickListener mOnClickListener;
    private Context mContext;
    private LayoutInflater mInflater;
    private static final int mLayout = R.layout.dialog_projection_item;

    public ProjectionAdapter(Context context, List<ProjectionListItem> items, View.OnClickListener listener) {
        super(context,mLayout,items);
        mContext  = context;
        mInflater = LayoutInflater.from(mContext);
        mOnClickListener = listener;
    }



    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = mInflater.inflate(mLayout,null,false);
            viewHolder = new ViewHolder();
            viewHolder.text = (TextView) convertView.findViewById(R.id.txt_view_projection_name);
            viewHolder.icon = (ImageView) convertView.findViewById(R.id.img_view_projicon_icon);
            viewHolder.checked = (ImageView) convertView.findViewById(R.id.img_view_selected);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        ProjectionListItem _item = getItem(position);
        if (_item.getSelected()) {
            viewHolder.checked.setVisibility(View.VISIBLE);
        } else {
            viewHolder.checked.setVisibility(View.GONE);
        }
        viewHolder.text.setText(_item.getTitle());
        viewHolder.icon.setImageResource(_item.getResource());



        mAllImgs.put(position,viewHolder.checked);

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeState(position);
                if (mOnClickListener!=null) {
                    mOnClickListener.onClick(v);
                }
            }
        });



        return convertView;
    }

    static class ViewHolder {
        TextView text;
        ImageView icon;
        ImageView checked;
    }

    private void changeState(int position) {
        SPUtils.put(mContext, Definition.KEY_PROJECTION,position);

        LogUtils.i("Projection", "position :" + position);

        for (int i = 0; i < getCount(); i++) {
            if (i == position) {
                getItem(i).setSelected(true);
            } else {
                getItem(i).setSelected(false);
            }
        }

        notifyDataSetChanged();
    }

}


